﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Smart.Pages
{
    public partial class BMIandBodyFatCalc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string heightStr = HeightTextBox.Text;
            string weightStr = WeightTextBox.Text;
            double height = 0, weight = 0;
            try
            {
                height = Double.Parse(heightStr);
                weight = Double.Parse(weightStr);

                if (height <= 0 || weight <= 0)
                    throw new Exception();

                double bmi = Global.BodyFatCalculatorService.GetBMI(height, weight);
                String result = "Вашият индекс на телесна маса е " + bmi.ToString();
                result += ". Вашият статус е " + Global.BodyFatCalculatorService.GetBMIValue(bmi);

                BMILabel.Text = result;
                BMILabel.ForeColor = Color.Black;
            }
            catch (Exception)
            {
                BMILabel.Text = "Моля въведете коректни стойности за височина и тегло!";
                BMILabel.ForeColor = Color.Red;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string heightStr = HeightTextBox.Text;
            string weightStr = WeightTextBox.Text;
            string ageStr = AgeTextBox.Text;
            string genderStr = GenderDropDownList.SelectedValue;

            double height = 0, weight = 0;
            int age = 0;
            bool isMale = false;

            try
            {
                height = Double.Parse(heightStr);
                weight = Double.Parse(weightStr);
                age = Int32.Parse(ageStr);
                if (genderStr.Equals("Male"))
                    isMale = true;

                if (height <= 0 || weight <= 0 || age < 0 || genderStr.Equals(""))
                    throw new Exception();

                double bmi = Global.BodyFatCalculatorService.GetBMI(height, weight);
                double bodyFat = Global.BodyFatCalculatorService.GetBodyFat(bmi, age, isMale);
                string result = "Имате " + (int)bodyFat + "% съдържание на мазнини в тялото. Вашият статус е " +
                    Global.BodyFatCalculatorService.GetBodyFatValue(bodyFat, isMale);

                BodyFatLabel.Text = result;
                BodyFatLabel.ForeColor = Color.Black;
            }
            catch (Exception)
            {
                BodyFatLabel.Text = "Моля въведете коректни стойности за височина, тегло, години и пол!";
                BodyFatLabel.ForeColor = Color.Red;
            }
        }
    }
}